home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / prog / plane / order.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  1KB  |  57 lines

  1.  
  2. // This programs is using dictionaries with keys of type "point" with two 
  3. // different linear orders, the lexicographic ordering on the cartesian 
  4. // coordinates (default ordering) and the lexicographic ordering on the
  5. // polar coordinates.
  6.  
  7. // must be linked with   libP.a libG.a libL.a -lm   !!!
  8.  
  9.  
  10. #include <LEDA/dictionary.h>
  11. #include <LEDA/plane.h>
  12.  
  13.  
  14. int pol_cmp(point x, point y) 
  15. {
  16.   // defines lexicographic order of the polar coordinates
  17.   
  18.   point origin(0,0);
  19.   segment sx(origin,x), sy(origin,y);
  20.   int c = compare(sx.angle(), sy.angle());  // predefined compare(double,double)
  21.   if (c) return c;
  22.   return compare(sx.length(),sy.length()); 
  23.   
  24.  }
  25.  
  26.  
  27. DEFINE_LINEAR_ORDER(point,pol_cmp,point_pol)
  28.  
  29. // Now "point_pol" is equivalent to the data type  point
  30. // with the linear order defined by "pol_cmp".
  31.  
  32.  
  33.  
  34.  
  35.  
  36. main()
  37. {
  38.   dictionary<point,int>      D;
  39.   dictionary<point_pol,int>  D_pol;
  40.  
  41.   point x;
  42.  
  43.   while (cin >> x) 
  44.   { D.insert(x,0);
  45.     D_pol.insert(x,0);
  46.    }
  47.  
  48.   dic_item it;
  49.  
  50.   forall_items(it,D) cout << D.key(it)  << "\n";
  51.   newline;
  52.  
  53.   forall_items(it,D_pol) cout << D_pol.key(it)  << "\n";
  54.   newline;
  55.  
  56. }
  57.